<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: App.class.php 2702 2012-02-02 12:35:01Z liu21st $


/**
 +------------------------------------------------------------------------------
 * ThinkPHP 应用程序类 精简模式
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Think
 * @subpackage  Core
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id: App.class.php 2702 2012-02-02 12:35:01Z liu21st $
 +------------------------------------------------------------------------------
 */
class App {
	
	/**
     +----------------------------------------------------------
	 * 运行应用实例 入口文件使用的快捷方法
     +----------------------------------------------------------
	 * @access public
     +----------------------------------------------------------
	 * @return void
     +----------------------------------------------------------
	 */
	static public function run() {
		// 设置系统时区
		date_default_timezone_set ( C ( 'DEFAULT_TIMEZONE' ) );
		// 加载动态项目公共文件和配置
		load_ext_file ();
		// 项目初始化标签
		tag ( 'app_init' );
		// URL调度
		Dispatcher::dispatch ();
		// 项目开始标签
		tag ( 'app_begin' );
		// Session初始化 支持其他客户端
		if (isset ( $_REQUEST [C ( "VAR_SESSION_ID" )] ))
			session_id ( $_REQUEST [C ( "VAR_SESSION_ID" )] );
		if (C ( 'SESSION_AUTO_START' ))
			session_start ();
		
		// 记录应用初始化时间
		if (C ( 'SHOW_RUN_TIME' ))
			G ( 'initTime' );
		App::exec ();
		// 项目结束标签
		tag ( 'app_end' );
		// 保存日志记录
		if (C ( 'LOG_RECORD' ))
			Log::save ();
		return;
	}
	
	/**
     +----------------------------------------------------------
	 * 执行应用程序
     +----------------------------------------------------------
	 * @access public
     +----------------------------------------------------------
	 * @return void
     +----------------------------------------------------------
	 * @throws ThinkExecption
     +----------------------------------------------------------
	 */
	static public function exec() {
		// 安全检测
		if (! preg_match ( '/^[A-Za-z_0-9]+$/', MODULE_NAME )) {
			throw_exception ( L ( '_MODULE_NOT_EXIST_' ) );
		}
		//创建Action控制器实例
		$group = defined ( 'GROUP_NAME' ) ? GROUP_NAME . '/' : '';
		$module = A ( $group . MODULE_NAME );
		if (! $module) {
			// 是否定义Empty模块
			$module = A ( "Empty" );
			if (! $module)
				// 模块不存在 抛出异常
				throw_exception ( L ( '_MODULE_NOT_EXIST_' ) . MODULE_NAME );
		}
		//执行当前操作
		call_user_func ( array (&$module, ACTION_NAME ) );
		return;
	}
}